package cobersky.engine.media
{
	import cobersky.engine.core.IWaveViewer;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	public class SoundWave extends Sprite
	{
		private var _byte:ByteArray;
		private var _isWave:Boolean;
		private var _fc:Class;
		private var _viewer:IWaveViewer;
		private var shape:Shape=new Shape;
		public function SoundWave(viewer:IWaveViewer)
		{		
			_byte=new ByteArray;
			_isWave=true;
			_viewer=viewer;
			shape.graphics.beginFill(0xffaa00);
			shape.graphics.drawCircle(0,0,10);
			shape.graphics.endFill();
			shape.x=100;
			shape.y=300;
			addChild(shape);
		}

		public function set viewer(value:IWaveViewer):void
		{
			_viewer = value;
		}

		public function get isWave():Boolean
		{
			return _isWave;
		}
		public function init():void{
			this.addChild(_viewer as DisplayObject);
		}
		public function set isWave(value:Boolean):void
		{
			_isWave = value;
		}

		public function start():void{
			addEventListener(Event.ENTER_FRAME,update);
		}
		
		protected function update(event:Event):void
		{
			SoundMixer.computeSpectrum(_byte,true);
			_viewer.update(_byte);
			if((_viewer as WaveView).radio<13){
				shape.scaleX-=0.05;
				shape.scaleY-=0.05;
			}else{
				shape.scaleX=shape.scaleY=(_viewer as WaveView).radio*.1;
			}
			
		}
		public function stop():void{
			removeEventListener(Event.ENTER_FRAME,update);
		}
	}
}